//
//  YKInputView.m
//  YunKang
//
//  Created by leoking870 on 2017/9/29.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKInputView.h"
#import "YKInsetTextField.h"
#import <UITextField+TFCore.h>
#import <ActionSheetPicker-3.0/ActionSheetDatePicker.h>
@interface YKInputView ()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *inputTextField;

@property (nonatomic, strong)NSDateFormatter *formatter;
@end

@implementation YKInputView
- (void)dealloc {
    [self.inputTextField removeTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
- (instancetype)initWithTitle:(NSString *)title input:(NSString *)input { 
    if (self = [super init]) {
        _contentTopInset = 8;
        _editable = YES;
        _inputBackgroundColor =  [UIColor whiteColor];
        _disableInputBackgroundColor = [UIColor tf_colorWithHexString:@"#f2f2f2"];
        _inputTextColor = [UIColor tf_colorWithHexString:@"#444444"];
        _titleColor = [UIColor tf_colorWithHexString:@"#444444"];
        _titleFont = [UIFont systemFontOfSize:14];
        _inputTextFont = [UIFont systemFontOfSize:14];
        
        [self addSubview:self.titleLabel];
        [self addSubview:self.inputTextField];
        self.titleLabel.text = title;
        self.inputTextField.text = input;
        self.titleLabel.tf_weight = 1;
        self.inputTextField.tf_weight = 3;
        self.inputTextField.font = self.inputTextFont;
        self.inputTextField.delegate = self;
        [self.inputTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self.subviews mas_alignWeightlyAlongAxis:MASAxisTypeHorizontal fixedSpace:@8 leadingTo:self.mas_leading leadSpace:13 trailingTo:self.mas_trailing tailSpace:13];
        [self.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top).offset(_contentTopInset);
            make.centerY.equalTo(self.mas_centerY);
            make.height.mas_equalTo(30);
        }];
    }
    return self;
}

- (void)textFieldDidChange:(id)textFieldDidChange {
    if (self.maxInputCount == 0) {
        return;
    }
    NSString *toBeString = self.inputTextField.text;
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; //ios7之前使用[UITextInputMode currentInputMode].primaryLanguage
    if ([lang isEqualToString:@"zh-Hans"]) { //中文输入
        UITextRange *selectedRange = [self.inputTextField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [self.inputTextField positionFromPosition:selectedRange.start offset:0];
        if (!position) {// 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (toBeString.length > self.maxInputCount) {
                self.inputTextField.text = [toBeString substringToIndex:self.maxInputCount];
            }
        }
        else{//有高亮选择的字符串，则暂不对文字进行统计和限制
        }
    }else{//中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        if (toBeString.length > self.maxInputCount) {
            self.inputTextField.text = [toBeString substringToIndex:self.maxInputCount];
        }
    }
}

- (void)setContentTopInset:(CGFloat)contentTopInset {
    _contentTopInset = contentTopInset;
    [self.subviews mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(_contentTopInset);

    }];
}

- (void)setInput:(NSString *)input {
    _inputTextField.text = input;
}

- (NSString *)input {
    return _inputTextField.text;
}

- (void)setKeyboardType:(UIKeyboardType)keyboardType {
    _inputTextField.keyboardType = keyboardType;
}
- (UIKeyboardType)keyboardType {
    return _inputTextField.keyboardType;
}

- (void)layoutSubviews {
    self.inputTextField.font = self.inputTextFont;
    self.inputTextField.textColor = self.inputTextColor;
    self.inputTextField.enabled = self.editable;
    [super layoutSubviews];
}

- (void)setInputBackgroundColor:(UIColor *)inputBackgroundColor {
    _inputBackgroundColor = inputBackgroundColor;
    self.inputTextField.backgroundColor = inputBackgroundColor;
    [self setNeedsLayout];
}

- (void)setInputTextColor:(UIColor *)inputTextColor {
    _inputTextColor = inputTextColor;
    self.inputTextField.textColor = inputTextColor;
    [self setNeedsLayout];
}
- (void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    self.titleLabel.textColor = titleColor;
    [self setNeedsLayout];
}

- (void)setTitleFont:(UIFont *)titleFont {
    _titleFont = titleFont;
    self.titleLabel.font = titleFont;
    [self setNeedsLayout];
}

- (void)setInputTextFont:(UIFont *)inputTextFont {
    _inputTextFont = inputTextFont;
    self.inputTextField.font = inputTextFont;
    [self setNeedsLayout];
}

- (void)setEditable:(BOOL)editable {
    _editable = editable;
    self.inputTextField.enabled = editable;
    self.inputTextField.backgroundColor = editable?self.inputBackgroundColor:self.disableInputBackgroundColor;
    self.inputTextField.borderStyle = editable?UITextBorderStyleRoundedRect:UITextBorderStyleNone;
    [self setNeedsLayout];
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = self.titleColor;
        _titleLabel.font = self.titleFont;
    }
    return _titleLabel;
}

- (UITextField *)inputTextField {
    if (!_inputTextField) {
        _inputTextField = [[UITextField alloc] init];
        _inputTextField.backgroundColor = self.inputBackgroundColor;
        _inputTextField.tf_cornerRadius = 3.0f;
//        _inputTextField.contentInsets = UIEdgeInsetsMake(0, 8, 0, 8);
        [_inputTextField tf_insetLeftSpace:8];
    }
    return _inputTextField;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (self.type == YKInputViewTypeString) {
        return YES;
    }
    else if (self.type == YKInputViewTypeDate) {
        
        self.formatter = [[NSDateFormatter alloc]init];
        self.formatter.dateFormat = self.dateFormat;
        NSDate *date = [self.formatter dateFromString:self.input];
        if (!date) {
            date = [NSDate date];
        }
        
        [ActionSheetDatePicker showPickerWithTitle:self.titleLabel.text datePickerMode:UIDatePickerModeDate selectedDate:date doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
            NSDate *date2 = selectedDate;
            self.input = [self.formatter stringFromDate:date2];
        } cancelBlock:^(ActionSheetDatePicker *picker) {
            
        } origin:[UIApplication sharedApplication].keyWindow];
        return NO;
    }
    return YES;
}


@end
